import { ref, computed } from 'vue'
import type { ChartData, ChartSeries, MockDataConfig } from '../types'

export function useChartData() {
  // 生成模拟时间序列数据
  const generateTimeSeriesData = (config: MockDataConfig): ChartData[] => {
    const { startDate, endDate, dataPoints } = config
    const start = new Date(startDate)
    const end = new Date(endDate)
    const timeStep = (end.getTime() - start.getTime()) / (dataPoints - 1)
    
    const data: ChartData[] = []
    for (let i = 0; i < dataPoints; i++) {
      const date = new Date(start.getTime() + i * timeStep)
      const value = Math.random() * 4 + Math.sin(i * 0.1) * 2 // 模拟波动数据
      data.push({
        date: date.toISOString().split('T')[0],
        value: Math.round(value * 100) / 100
      })
    }
    return data
  }

  // 生成百分比数据 (0-4%)
  const generatePercentageData = (config: MockDataConfig): ChartData[] => {
    const { startDate, endDate, dataPoints } = config
    const start = new Date(startDate)
    const end = new Date(endDate)
    const timeStep = (end.getTime() - start.getTime()) / (dataPoints - 1)
    
    const data: ChartData[] = []
    for (let i = 0; i < dataPoints; i++) {
      const date = new Date(start.getTime() + i * timeStep)
      const value = Math.random() * 4 + Math.sin(i * 0.05) * 1.5 // 模拟利率数据
      data.push({
        date: date.toISOString().split('T')[0],
        value: Math.round(value * 100) / 100
      })
    }
    return data
  }

  // 生成比率数据 (100-170)
  const generateRatioData = (config: MockDataConfig): ChartData[] => {
    const { startDate, endDate, dataPoints } = config
    const start = new Date(startDate)
    const end = new Date(endDate)
    const timeStep = (end.getTime() - start.getTime()) / (dataPoints - 1)
    
    const data: ChartData[] = []
    for (let i = 0; i < dataPoints; i++) {
      const date = new Date(start.getTime() + i * timeStep)
      const value = 100 + Math.random() * 70 + Math.sin(i * 0.03) * 20 // 模拟比率数据
      data.push({
        date: date.toISOString().split('T')[0],
        value: Math.round(value * 100) / 100
      })
    }
    return data
  }

  // 生成周期数据 (-15到25)
  const generateCycleData = (config: MockDataConfig): ChartData[] => {
    const { startDate, endDate, dataPoints } = config
    const start = new Date(startDate)
    const end = new Date(endDate)
    const timeStep = (end.getTime() - start.getTime()) / (dataPoints - 1)
    
    const data: ChartData[] = []
    for (let i = 0; i < dataPoints; i++) {
      const date = new Date(start.getTime() + i * timeStep)
      const value = Math.sin(i * 0.1) * 20 + Math.random() * 5 // 模拟周期数据
      data.push({
        date: date.toISOString().split('T')[0],
        value: Math.round(value * 100) / 100
      })
    }
    return data
  }

  // 生成货币信用周期数据 (-20%到20%)
  const generateCurrencyCreditData = (config: MockDataConfig): ChartData[] => {
    const { startDate, endDate, dataPoints } = config
    const start = new Date(startDate)
    const end = new Date(endDate)
    const timeStep = (end.getTime() - start.getTime()) / (dataPoints - 1)
    
    const data: ChartData[] = []
    for (let i = 0; i < dataPoints; i++) {
      const date = new Date(start.getTime() + i * timeStep)
      const value = Math.sin(i * 0.15) * 20 + Math.random() * 3 // 模拟货币信用周期数据
      data.push({
        date: date.toISOString().split('T')[0],
        value: Math.round(value * 100) / 100
      })
    }
    return data
  }

  // 生成久期数据 (0-4)
  const generateDurationData = (config: MockDataConfig): ChartData[] => {
    const { startDate, endDate, dataPoints } = config
    const start = new Date(startDate)
    const end = new Date(endDate)
    const timeStep = (end.getTime() - start.getTime()) / (dataPoints - 1)
    
    const data: ChartData[] = []
    for (let i = 0; i < dataPoints; i++) {
      const date = new Date(start.getTime() + i * timeStep)
      const value = 2 + Math.sin(i * 0.2) * 1.5 + Math.random() * 0.5 // 模拟久期数据
      data.push({
        date: date.toISOString().split('T')[0],
        value: Math.round(value * 100) / 100
      })
    }
    return data
  }

  return {
    generateTimeSeriesData,
    generatePercentageData,
    generateRatioData,
    generateCycleData,
    generateCurrencyCreditData,
    generateDurationData
  }
}
